package com.rt.easyjava.base.biz1.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.biz1.bean.TmptAuthCompanyBiz;
import com.rt.easyjava.base.biz1.dto.TmptAuthCompanyBizDto;
import com.rt.easyjava.base.biz1.mapper.TmptAuthCompanyBizMapper;
import com.rt.easyjava.base.biz1.service.intf.TmptAuthCompanyBizService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-06-06
 */
public class TmptAuthCompanyServiceImpl extends ServiceImpl<TmptAuthCompanyBizMapper, TmptAuthCompanyBiz> implements TmptAuthCompanyBizService {
    private final Logger logger = LoggerFactory.getLogger(TmptAuthCompanyServiceImpl.class);

    @Autowired
    protected MyMapper mapper;
    @Resource
    protected TmptAuthCompanyBizMapper tmptAuthCompanyBizMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param tmptAuthCompanyBizDto
    * @return
    */
    @Override
    public Result<TmptAuthCompanyBizDto> create(TmptAuthCompanyBizDto tmptAuthCompanyBizDto) {
        Result<TmptAuthCompanyBizDto> result = new Result<>();

        // dto to entity
        TmptAuthCompanyBiz tmptAuthCompanyBiz = mapper.map(tmptAuthCompanyBizDto, TmptAuthCompanyBiz.class);

        tmptAuthCompanyBizMapper.insert(tmptAuthCompanyBiz);

        return result.success();
    }

    /**
    * update a object by Id
    * @param tmptAuthCompanyBizDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptAuthCompanyBizDto> update(TmptAuthCompanyBizDto tmptAuthCompanyBizDto, UpdateWrapper updateWrapper) {
        Result<TmptAuthCompanyBizDto> result = new Result<>();

        // dto to entity
        TmptAuthCompanyBiz tmptAuthCompanyBiz = mapper.map(tmptAuthCompanyBizDto, TmptAuthCompanyBiz.class);

        tmptAuthCompanyBizMapper.update(tmptAuthCompanyBiz, updateWrapper);

        return result.success();
    }

    /**
    * 分页 (无合计功能)
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptAuthCompanyBizDto> page(Page<TmptAuthCompanyBiz> page, QueryWrapper<TmptAuthCompanyBiz> queryWrapper){
        Result<TmptAuthCompanyBizDto> result = new Result<>();

        // mapper
        IPage<TmptAuthCompanyBiz> iPage = tmptAuthCompanyBizMapper.selectPage(page, queryWrapper);

        // 查询结果转换成DTO
        List<TmptAuthCompanyBizDto> tmptAuthCompanyBizDtoList= mapper.maplist(iPage.getRecords(), TmptAuthCompanyBizDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptAuthCompanyBizDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptAuthCompanyBizDto> selectBy(QueryWrapper<TmptAuthCompanyBiz> queryWrapper) {
        Result<TmptAuthCompanyBizDto> result = new Result<>();

        List<TmptAuthCompanyBiz> list = tmptAuthCompanyBizMapper.selectList(queryWrapper);

        List<TmptAuthCompanyBizDto> dtoList = mapper.maplist(list, TmptAuthCompanyBizDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptAuthCompanyBizDto> selectById(Long id) {
        Result<TmptAuthCompanyBizDto> result = new Result<>();

        TmptAuthCompanyBiz tmptAuthCompanyBiz = tmptAuthCompanyBizMapper.selectById(id);

        TmptAuthCompanyBizDto tmptAuthCompanyBizDto = mapper.map(tmptAuthCompanyBiz, TmptAuthCompanyBizDto.class);

        return result.setObj(tmptAuthCompanyBizDto).success();
    }




    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * 默认开启合计查询，按需注掉代码
    * @param page
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptAuthCompanyBizDto> myPage(Page<TmptAuthCompanyBiz> page, QueryWrapper<TmptAuthCompanyBiz> queryWrapper) {
        Result<TmptAuthCompanyBizDto> result = new Result();

        IPage<TmptAuthCompanyBiz> iPage = tmptAuthCompanyBizMapper.myPage(page, queryWrapper);
        // 查询结果转换成DTO
        List<TmptAuthCompanyBizDto> tmptAuthCompanyBizDtoList = mapper.maplist(iPage.getRecords(), TmptAuthCompanyBizDto.class);
        // 返回结果集
        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptAuthCompanyBizDtoList);

        // 合计
        List<TmptAuthCompanyBiz> sumList = tmptAuthCompanyBizMapper.mySelectSum(queryWrapper);
        List<TmptAuthCompanyBizDto> sumDtoList = mapper.maplist(sumList, TmptAuthCompanyBizDto.class);
        result.getMap().put("footer", sumDtoList);

        return result.success();
    }

    @Override
    public Result<TmptAuthCompanyBizDto> mySelectBy(QueryWrapper<TmptAuthCompanyBiz> queryWrapper) {
        Result<TmptAuthCompanyBizDto> result = new Result();

        List<TmptAuthCompanyBiz> list = tmptAuthCompanyBizMapper.mySelectBy(queryWrapper);
        List<TmptAuthCompanyBizDto> dtoList = mapper.maplist(list, TmptAuthCompanyBizDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptAuthCompanyBizDto> mySelectById(long id) {
        Result<TmptAuthCompanyBizDto> result = new Result();

        TmptAuthCompanyBiz tmptAuthCompanyBiz = tmptAuthCompanyBizMapper.mySelectById(id);
        TmptAuthCompanyBizDto dto = mapper.map(tmptAuthCompanyBiz,TmptAuthCompanyBizDto.class);
        result.setObj(dto);

        return result.success();
    }


}
